package com.ruoyi.edh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edh.domain.EdhMicroshop;
import com.ruoyi.edh.mapper.EdhMicroshopGoodsMapper;
import com.ruoyi.edh.mapper.EdhMicroshopMapper;
import com.ruoyi.edh.service.IEdhMicroshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 易订货-微店Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhMicroshopServiceImpl implements IEdhMicroshopService
{
    @Autowired
    private EdhMicroshopMapper edhMicroshopMapper;

    @Autowired
    private EdhMicroshopGoodsMapper edhMicroshopGoodsMapper;

    /**
     * 查询易订货-微店
     * 
     * @param userId 用户id
     * @return 易订货-微店
     */
    @Override
    public Map<String,Object> selectEdhMicroshopByUserId(Long userId)
    {
        Map<String,Object> result = new HashMap<String, Object>();

        EdhMicroshop microshop = edhMicroshopMapper.selectEdhMicroshopByUserId(userId);

        if (StringUtils.isNull(microshop)){throw new CustomException("未找到该用户的微店信息"); }
        List<Map<String, Object>> list = edhMicroshopGoodsMapper.selectMicroShopGoodsByShopId(microshop.getId());
        result.put("microshop",microshop);
        result.put("microshopgoods",list);
        return result;
    }

    /**
     * 查询易订货-微店
     *
     * @param userId    用户id
     * @param goodsName 商品名称
     * @return 易订货-微店
     */
    @Override
    public Map<String, Object> selectEdhMicroshopByUserId(Long userId, String goodsName) {
        Map<String,Object> result = new HashMap<String, Object>();

        EdhMicroshop microshop = edhMicroshopMapper.selectEdhMicroshopByUserId(userId);

        if (StringUtils.isNull(microshop)){throw new CustomException("未找到该用户的微店信息"); }
        List<Map<String, Object>> list = edhMicroshopGoodsMapper.selectMicroShopGoodsByShopIdAndGoodsName(microshop.getId(),goodsName);
        result.put("microshop",microshop);
        result.put("microshopgoods",list);
        return result;
    }

    /**
     * 查询易订货-微店列表
     * 
     * @param edhMicroshop 易订货-微店
     * @return 易订货-微店
     */
    @Override
    public List<EdhMicroshop> selectEdhMicroshopList(EdhMicroshop edhMicroshop)
    {
        return edhMicroshopMapper.selectEdhMicroshopList(edhMicroshop);
    }

    /**
     * 新增易订货-微店
     * 
     * @param edhMicroshop 易订货-微店
     * @return 结果
     */
    @Override
    public int insertEdhMicroshop(EdhMicroshop edhMicroshop)
    {
        edhMicroshop.setCreateTime(DateUtils.getNowDate());
        return edhMicroshopMapper.insertEdhMicroshop(edhMicroshop);
    }

    /**
     * 修改易订货-微店
     * 
     * @param edhMicroshop 易订货-微店
     * @return 结果
     */
    @Override
    public int updateEdhMicroshop(EdhMicroshop edhMicroshop)
    {
        edhMicroshop.setUpdateTime(DateUtils.getNowDate());
        return edhMicroshopMapper.updateEdhMicroshop(edhMicroshop);
    }

    /**
     * 批量删除易订货-微店
     * 
     * @param ids 需要删除的易订货-微店ID
     * @return 结果
     */
    @Override
    public int deleteEdhMicroshopByIds(Long[] ids)
    {
        return edhMicroshopMapper.deleteEdhMicroshopByIds(ids);
    }

    /**
     * 删除易订货-微店信息
     * 
     * @param id 易订货-微店ID
     * @return 结果
     */
    @Override
    public int deleteEdhMicroshopById(Long id)
    {
        return edhMicroshopMapper.deleteEdhMicroshopById(id);
    }
}
